package com.hcy.manager.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hcy.driver.entity.CarInfo;
import com.hcy.driver.entity.CarLevel;
import com.hcy.driver.entity.CarType;
import com.hcy.driver.entity.Info;
import com.hcy.driver.service.impl.CarInfoServiceImpl;
import com.hcy.driver.service.impl.CarLevelServiceImpl;
import com.hcy.driver.service.impl.CarTypeServiceImpl;
import com.hcy.manager.CarInfoBz;
import com.hcy.manager.dto.request.CarInfoAddRequestDTO;
import com.hcy.manager.dto.request.CarInfoRequestDTO;
import com.hcy.manager.dto.request.CarLevelRequestDTO;
import com.hcy.manager.dto.request.CarTypeRequestDTO;
import com.hcy.manager.dto.response.CarInfoResponseDTO;
import com.hcy.manager.dto.response.CarLevelResponseDTO;
import com.hcy.manager.dto.response.CarTypeResponseDTO;
import com.hcy.manager.dto.response.DriverInfoResponseDTO;
import com.hcy.rabbitbasicmodel.dto.MyPage;
import com.hcy.rabbitbasicutils.utils.CheckUtils;
import com.hcy.rabbitbasicutils.utils.GaoDeUtils;
import com.hcy.rabbitbasicutils.utils.SnowFlake;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author huchenying
 * @Description
 * @Date 2022/4/27
 **/
@Service
@Slf4j
public class CarInfoBzImpl implements CarInfoBz {

    @Autowired
    CarInfoServiceImpl carInfoService;
    @Autowired
    CarTypeServiceImpl carTypeService;
    @Autowired
    CarLevelServiceImpl carLevelService;
    @Autowired
    GaoDeUtils gaoDeUtils;

    @Override
    public MyPage<CarInfoResponseDTO> getCarList(CarInfoRequestDTO requestDTO) {
        LambdaQueryWrapper<CarInfo> queryWrapper = new LambdaQueryWrapper<>();
        // 必要条件
//        queryWrapper.ne(CarInfo::getOperationStatus,-1); // 只查询未删除的
        // 查询条件
        queryWrapper.eq(CheckUtils.checkVarIsNotNullOrEmpty(requestDTO.getId()), CarInfo::getId, requestDTO.getId());
        queryWrapper.like(CheckUtils.checkVarIsNotNullOrEmpty(requestDTO.getPlateNumber()), CarInfo::getPlateNumber, requestDTO.getPlateNumber());
        queryWrapper.like(CheckUtils.checkVarIsNotNullOrEmpty(requestDTO.getCityCode()), CarInfo::getCityCode, requestDTO.getCityCode());
        queryWrapper.like(CheckUtils.checkVarIsNotNullOrEmpty(requestDTO.getColor()), CarInfo::getColor, requestDTO.getColor());
        queryWrapper.like(CheckUtils.checkVarIsNotNullOrEmpty(requestDTO.getFullName()), CarInfo::getFullName, requestDTO.getFullName());
        queryWrapper.eq(CheckUtils.checkVarIsNotNullOrEmpty(requestDTO.getOperationStatus()), CarInfo::getOperationStatus, requestDTO.getOperationStatus());
        //时间
        queryWrapper.between(CheckUtils.checkVarIsNotNullOrEmpty(requestDTO.getPublishTimeStart()) && CheckUtils.checkVarIsNotNullOrEmpty(requestDTO.getPublishTimeEnd())
                , CarInfo::getPublishTime, requestDTO.getPublishTimeStart(), requestDTO.getPublishTimeEnd());
        //查询
        Page<CarInfo> page = new Page<>(requestDTO.getPageNO(), requestDTO.getPageSize(),true);
        Page<CarInfo> page1 = carInfoService.page(page, queryWrapper);
        MyPage<CarInfoResponseDTO> responseDTOMyPage = new MyPage<>(page1.getCurrent(), page1.getSize(), page1.getTotal());
        //组装
        List<CarInfoResponseDTO> driverInfoResponseDTOS = new ArrayList<>();
        for (CarInfo record : page1.getRecords()) {
            CarInfoResponseDTO driverInfoResponseDTO = new CarInfoResponseDTO();
            BeanUtils.copyProperties(record, driverInfoResponseDTO);
            driverInfoResponseDTOS.add(driverInfoResponseDTO);
        }
        responseDTOMyPage.setRecords(driverInfoResponseDTOS);
        return responseDTOMyPage;
    }

    @Override
    public void updateCarStatus(CarInfoRequestDTO requestDTO) {
        CarInfo carInfo = new CarInfo();
        carInfo.setId(requestDTO.getId());
        carInfo.setOperationStatus(requestDTO.getStatus());
        boolean b = carInfoService.updateById(carInfo);
        if(!b){
            throw new RuntimeException("删除失败");
        }
    }

    @Override
    public CarInfoResponseDTO insertOrUpdate(CarInfoAddRequestDTO requestDTO) {
        CarInfo carInfo = new CarInfo();
        // 调用高德创建终端
        if(requestDTO.getId() == null){
            Integer tid = gaoDeUtils.addCar(SnowFlake.getId());
            requestDTO.setId(tid);
        }
        BeanUtils.copyProperties(requestDTO,carInfo);
        boolean b = carInfoService.saveOrUpdate(carInfo);
        if(!b){
            throw new RuntimeException("添加失败");
        }
        CarInfoResponseDTO carInfoResponseDTO = new CarInfoResponseDTO();
        BeanUtils.copyProperties(carInfo,carInfoResponseDTO);
        return carInfoResponseDTO;
    }

    @Override
    public void deleteCarType(CarTypeRequestDTO requestDTO) {
        CarType carType = new CarType();
        carType.setId(requestDTO.getId());
        carType.setStatus(0);
        boolean b = carTypeService.updateById(carType);
        if(!b){
            throw new RuntimeException("删除失败");
        }
    }

    @Override
    public List<CarTypeResponseDTO> getCarTypeList() {
        LambdaQueryWrapper<CarType> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CarType::getStatus,1);
        List<CarType> list = carTypeService.list(queryWrapper);
        List<CarTypeResponseDTO> responseDTOS = new ArrayList<>();
        for (CarType carType : list) {
            CarTypeResponseDTO carTypeResponseDTO = new CarTypeResponseDTO();
            BeanUtils.copyProperties(carType,carTypeResponseDTO);
            responseDTOS.add(carTypeResponseDTO);
        }
        return responseDTOS;
    }

    @Override
    public CarTypeResponseDTO insertOrUpdateCarType(CarTypeRequestDTO requestDTO) {
        CarType carType = new CarType();
        BeanUtils.copyProperties(requestDTO,carType);
        boolean b = carTypeService.saveOrUpdate(carType);
        if(!b){
            throw new RuntimeException("更新失败");
        }
        CarTypeResponseDTO responseDTO = new CarTypeResponseDTO();
        BeanUtils.copyProperties(carType,responseDTO);
        return responseDTO;
    }

    @Override
    public void deleteCarLevel(CarLevelRequestDTO requestDTO) {
        CarLevel carLevel = new CarLevel();
        carLevel.setId(requestDTO.getId());
        carLevel.setEnable(0);
        boolean b = carLevelService.updateById(carLevel);
        if(!b){
            throw new RuntimeException("删除失败");
        }
    }

    @Override
    public List<CarLevelResponseDTO> getCarLevelList() {
        LambdaQueryWrapper<CarLevel> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CarLevel::getEnable,1);
        List<CarLevel> list = carLevelService.list(queryWrapper);
        List<CarLevelResponseDTO> responseDTOS = new ArrayList<>();
        for (CarLevel carLevel : list) {
            CarLevelResponseDTO carLevelResponseDTO = new CarLevelResponseDTO();
            BeanUtils.copyProperties(carLevel,carLevelResponseDTO);
            responseDTOS.add(carLevelResponseDTO);
        }
        return responseDTOS;
    }

    @Override
    public CarLevelResponseDTO insertOrUpdateCarLevel(CarLevelRequestDTO requestDTO) {
        CarLevel carLevel = new CarLevel();
        BeanUtils.copyProperties(requestDTO,carLevel);
        boolean b = carLevelService.saveOrUpdate(carLevel);
        if(!b){
            throw new RuntimeException("更新失败");
        }
        CarLevelResponseDTO responseDTO = new CarLevelResponseDTO();
        BeanUtils.copyProperties(carLevel,responseDTO);
        return responseDTO;
    }

}
